<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单配送ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

//查询订单主表类
quanli_require('module/base/bill/billSearch.class.php');

//查询订单商品生产类
quanli_require('module/base/bill/billProduct.class.php');

//查询配送员类
quanli_require('module/base/system/sysDistributionTechnician.class.php');

//查询配送站类
quanli_require('module/base/system/sysDistribution.class.php');
//add by yim.hu 20141113 FUN-BK-0030 订单分配页面增加标识退货的订单--start
quanli_require('module/base/bill/billReturn.class.php');
//add by yim.hu 20141113 FUN-BK-0030 订单分配页面增加标识退货的订单--end
class ajax_orderAllocation {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1:
                    $this->orderAllocationList();//查询订单主表信息
                    break;

                case 2:
                    $this->billMasterSearch(); //批量分配 根据订单号查询订单主表信息 校验是否分配配送站 是否分配配送员 查询条件配送站与列表配送站是否匹配
                    break;

                case 3:
                    $this->sysDistribution();//弹出层中根据配送站id查询配送员
                    break;

                case 4:
                    $this->sysDistribution_technician();//弹出层中根据配送员id查询配送员手机号

                case 5:
                    $this->addDistribution_technician();//执行批量分配配送员


                case 6:
                    $this->billMasterSearchEdit(); //分配修改 根据订单号查询订单主表信息 校验是否分配配送站 是否分配配送员
                    break;

                case 7:
                    $this->editDistribution_technician();//执行分配修改配送员

                case 8:
                    $this->billMasterSearch_submenu(); //分单调度 根据订单号查询订单主表信息 校验是否分配配送站 是否分配配送员
                    break;

                case 9:
                    $this->getDistribution(); //获得配送站信息
                    break;

                case 10:
                    $this->submenuExecute();//分单调度执行
                    break;
            }
        }
    }



    /**
     * 查询订单主表信息
     */
    private function orderAllocationList() {
        //mod by allen.qiang 20141113 删除无用代码 start
        //global $G_PGLOB;
        global $G_SHOP;
        $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');
        //mod by allen.qiang 20141113 删除无用代码 end
        //查询条件选择了城市、配送站 则根据选择的城市、配送站查询
        if( isset($_REQUEST['deliver_cityid']) && is_numeric($_REQUEST['deliver_cityid']) && isset( $_REQUEST['distribution_id']) && is_numeric($_REQUEST['distribution_id'])){
            $_REQUEST['deliver_cityid'] = $_REQUEST['deliver_cityid'];
            $_REQUEST['distribution_id'] = $_REQUEST['distribution_id'];

            //查询条件选择了城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的配送站查询
        }else if(isset($_REQUEST['deliver_cityid']) && is_numeric($_REQUEST['deliver_cityid']) && !isset( $_REQUEST['distribution_id']) || !is_numeric($_REQUEST['distribution_id']) ){
            $_REQUEST['deliver_cityid'] = $_REQUEST['deliver_cityid'];
            //del by allen.qiang 20141113 删除无用代码 start
            //global $G_SHOP;
            //$PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');
            //del by allen.qiang 20141113 删除无用代码 end

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                //del by allen.qiang 20141113 删除无用代码 start
                //global $G_SHOP;
                //$PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');
                //del by allen.qiang 20141113 删除无用代码 end
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }

            //查询条件没有选择城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的城市、配送站查询
        }else{
            //del by allen.qiang 20141113 删除无用代码 start
            //global $G_SHOP;
            //$PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');
            //del by allen.qiang 20141113 删除无用代码 end

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }
        }

        //实例用户类
        $billSearch = new billSearch();

        //查询订单信息
        $bill_array = $billSearch->searchQueryBill( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($bill_array) {

            foreach ($bill_array as &$bill) {

                //过滤顺丰马卡龙订单 Add By allen.qiang 2015-05-07
                if(date('Y-m-d') > '2015-05-06' && $bill['billPromotion']=='495' )
                    continue;

                //订单标识
                if($bill['is_urgent'] == 1){
                    $isUrgent = "加急";
                }else if($bill['is_urgent'] == 2){
                    $isUrgent = "重要";
                }else{
                    $isUrgent = "普通";
                }

                if($bill['billType'] == 1){
                    $billType = "正常订单";
                }else if($bill['billType'] == 2){
                    $billType = "生日会蛋糕";
                }else if($bill['billType'] == 3){
                    $billType = "市场推广";
                }else if($bill['billType'] == 4){
                    $billType = "其他订单";
                }else if($bill['billType'] == 5){
                    $billType = "补偿订单";
                }else if($bill['billType'] == 6){
                    $billType = "测试订单";
                }else if($bill['billType'] == 7){
                    $billType = "招待赠送";
                }else if($bill['billType'] == 8){
                    $billType = "大客户订单";
                }else{
                    $billType = "试吃订单";
                }

                //mod by yim.hu 20141121 FUN-BK-0035-订单状态优化 --start
//                 if($bill['bill_status'] == 0){
//                     $bill_status = "建立";
//                 }else if($bill['bill_status'] == 1){
//                     $bill_status = "确认";
//                 }else if($bill['bill_status'] == 2){
//                     $bill_status = "生产";
//                 }else if($bill['bill_status'] == 3){
//                     $bill_status = "入库";
//                 }else if($bill['bill_status'] == 4){
//                     $bill_status = "出库";
//                 }else if($bill['bill_status'] == 5){
//                     $bill_status = "配送站收货";
//                 }else if($bill['bill_status'] == 6){
//                     $bill_status = "配送";
//                 }else if($bill['bill_status'] == 7){
//                     $bill_status = "完成";
//                 }
                if($bill['bill_status'] == 0){
                	$bill_status = "未确认";
                }else if($bill['bill_status'] == 1){
                	$bill_status = "已确认";
                }else if($bill['bill_status'] == 2){
                	$bill_status = "已打印生产单";
                }else if($bill['bill_status'] == 3){
                	$bill_status = "已质检入库";
                }else if($bill['bill_status'] == 4){
                	$bill_status = "已上车";
                }else if($bill['bill_status'] == 5){
                	$bill_status = "已下车";
                }else if($bill['bill_status'] == 6){
                	$bill_status = "已出仓";//暂时没
                }else if($bill['bill_status'] == 7){
                	$bill_status = "已完成";
                }
                //mod by yim.hu 20141121 FUN-BK-0035-订单状态优化 --end

                if($bill['pay_type'] == 1){
                    $payTypeName = $bill['payTypeName'];
                }else{
                    $payTypeName = "";
                }

                //add by yim.hu 20141113 FUN-BK-0030 订单分配页面增加标识退货的订单--start
                //实例订单退换货类
                $billReturn = new billReturn( $G_SHOP->DBCA );
                $ReturnKind= "<font face=宋体 color=#13CB00>".$billReturn->GetReturnKind($bill['bill_code'])."</font>";
                //add by allen.qiang 20141113 FUN-BK-0030 订单分配页面增加标识退货的订单--end
                    $result_array['rows'][] = array(
                        'id' => (string)$bill['bill_no'],
                        'cell' => array(
                           '<input type="checkbox" class="itemCheck" name="billArr[]" value="' . $bill['bill_no'] . '" />',
                            '<a href="javascript: void(0);" onclick="orderAllocation.billview(\''.$bill['bill_no'].'\');">'.$bill['bill_code'].'</a>',
                            $isUrgent,
                        	//add by yim.hu 20141113 FUN-BK-0030 订单分配页面增加标识退货的订单--start
                        	$ReturnKind,
                        	//add by yim.hu 20141113 FUN-BK-0030 订单分配页面增加标识退货的订单--end                     		
                            $billType,
                            $bill_status,
                            $bill['send_date'],
                            $bill['send_start_time'],
                            $bill['send_end_time'],
                            //add by allen.qiang 20140925 订单分配界面需要加字段：下单时间--start
                            $bill['createTime'],
                            //add by allen.qiang 20140925 订单分配界面需要加字段：下单时间--end
                            $bill['distribution_name'],
                            $bill['distribution_technicia'],
                            $bill['deliver_city'],
                            $bill['deliver_county'],
                            $bill['deliver_address'],
                            $bill['deliver_name'],
                            $bill['deliver_mobile'],
                            $bill['deliver_phone'],
                            $bill['invoice'] == 1 ? '是' : '否',
                            $bill['bill_totalAmount'],
                            $bill['goods_spec_str'],
                            $payTypeName,
                            $bill['bill_receivable_money'],
                            $bill['remark']
                        )
                    );

            }
        }

        echo json_encode($result_array);
    }


    /*
     *
     * 批量分配 根据配送站id查询订单主表信息 校验是否分配配送站 是否分配配送员 查询条件中配送站与列表中配送站是否匹配
     */
    public function billMasterSearch(){

        global $G_PGLOB;

        if(!isset($_REQUEST['billNoArr'],$_REQUEST['distribution_id'])){
            returnJsonData('缺少参数',400);
        }

        if(!is_array($_REQUEST['billNoArr'])){
            returnJsonData('参数错误',400);
        }

        $notFoundArr = array();//未找到该订单号数组
        $notAllocationArr = array();//未分配配送站数组
        $yesAllocationArr = array();//已分配配送站数组
        $notMatchingArr = array();//查询条件中配送站与列表中配送站不匹配数组
        foreach($_REQUEST['billNoArr'] as $bill_no){

            //实例化订单主表
            $bill_master_obj = new entity('bill_master');
            $bill_master_obj->bill_no =$bill_no;

            if(!$G_PGLOB->DBCA->fetch($bill_master_obj)){
                $notFoundArr[] = $bill_no;
            }

            if($bill_master_obj->distribution_id != $_REQUEST['distribution_id']){
                $notMatchingArr[] = $bill_no;
            }

            if(empty($bill_master_obj->distribution_id) || $bill_master_obj->distribution_id == 0){
                $notAllocationArr[] = $bill_no;
            }

            if(isset($bill_master_obj->distribution_technician_id) && $bill_master_obj->distribution_technician_id > 0){
                $yesAllocationArr[] = $bill_no;
            }




        }
        if( $notFoundArr ){
            $res = implode('], [', $notFoundArr);
            returnJsonData('订单号['. $res . ']未找到该订单信息',400);
        }

        if( $notAllocationArr ){
            $res = implode('], [', $notAllocationArr);
            returnJsonData('订单号['. $res . ']未分配配送站，不能批量分配',400);
        }

        if( $yesAllocationArr ){
            $res = implode('], [', $yesAllocationArr);
            returnJsonData('订单号['. $res . ']已分配配送员，不能重复分配',400);
        }
        if( $notMatchingArr ){
            $res = implode('], [', $notMatchingArr);
            returnJsonData('订单号['. $res . ']查询条件中配送站与列表中配送站不匹配，请在查询条件里选择城市和配送站进行查询后再使用本操作',400);
        }
        returnJsonData('ok',200);
    }


    /*
     *
     * 弹出层中根据配送站id查询配送员
     */
    public  function sysDistribution (){

        global $G_PGLOB;

        if(!isset($_REQUEST['distribution_id'])){
            returnJsonData('缺少参数',400);
        }

        if(!is_numeric($_REQUEST['distribution_id'])){
            returnJsonData('参数错误',400);
        }

        //实例化配送员类
        $sysDistributionTechnician = new sysDistributionTechnician();

        $arg_array =  array(
            'distribution_id'=>$_REQUEST['distribution_id'],
            'useFlg'=>1
        );
        $result = $sysDistributionTechnician->search( $arg_array );

        if($result){
            returnJsonData('ok',200,$result);
        }else{
            returnJsonData('未找到配送员信息',400);
        }

    }

    /*
     * 弹出层中根据配送员id查询配送员手机号
     *
     */
    public function sysDistribution_technician(){
        global $G_PGLOB;

        if(!isset($_REQUEST['distribution_technician_id'])){
            returnJsonData('缺少参数',400);
        }

        if(!is_numeric($_REQUEST['distribution_technician_id'])){
            returnJsonData('参数错误',400);
        }

        //实例化配送员类
        $sysDistributionTechnician = new sysDistributionTechnician();

        $arg_array =  array(
            'distribution_technician_id'=>$_REQUEST['distribution_technician_id']
        );
        $result = $sysDistributionTechnician->search( $arg_array );

        if($result){
            returnJsonData('ok',200,$result);
        }else{
            returnJsonData('未找到配送员信息',400);
        }
    }

    /*
     *
     * 执行批量分配配送员
     */
    public function addDistribution_technician(){

        global $G_PGLOB;

        if(!isset($_REQUEST['distribution_technician_id'],$_REQUEST['mobile'],$_REQUEST['billNoArr'],$_REQUEST['distribution_technicia'])){

            returnJsonData('缺少参数',400);
        }

        if(!is_numeric($_REQUEST['distribution_technician_id'])){
            returnJsonData('参数错误',400);
        }

        if(!is_array($_REQUEST['billNoArr'])){
            returnJsonData('参数错误',400);
        }

        //实例化配送员类 校验配送员信息
        $sysDistributionTechnician = new sysDistributionTechnician();

        $arg_array =  array(
            'distribution_technician_id'=>$_REQUEST['distribution_technician_id'],
            'mobile'=>$_REQUEST['mobile']
        );
        $result = $sysDistributionTechnician->search( $arg_array );

        if(empty($result)){
            returnJsonData('未找到对应配送员信息',400);
        }
        try{
            //开启事务
            $G_PGLOB->DBCA->beginTransaction();

            foreach($_REQUEST['billNoArr'] as $bill_no){

                //实例化订单主表
                $bill_master_obj = new entity('bill_master');
                $bill_master_obj->bill_no =$bill_no;
                if(!$G_PGLOB->DBCA->fetch($bill_master_obj)){
                    throw new Exception('未找到该订单主表信息',400);
                }
                if($bill_master_obj->bill_status == 7){
                    throw new Exception('已完成订单，无需分配',400);
                }
                //add by yim.hu 20141121 FUN-BK-0035-状态不做修改 --start
                
                	$bill_status = $bill_master_obj->bill_status;
                	
               
                //add by yim.hu 20141121 FUN-BK-0035-状态不做修改 --end
                $bill_master_obj->distribution_technician_id = $_REQUEST['distribution_technician_id'];
                $bill_master_obj->distribution_technicia = $_REQUEST['distribution_technicia'];
                $G_PGLOB->DBCA->save($bill_master_obj);
            /*=======================================================
             * 写入日志 start
             * ======================================================
             */
                quanli_include('module/base/customer/logHandler.class.php');
                //实例日志类
                $logHandler = new logHandler( $G_PGLOB->DBCA );

                $log_array = array(
                    'logCategoryID' => 1,
                    'logItem' => $bill_master_obj->bill_no,
                    'extend_1' => '订单分配配送员',
                    'extend_2' => '配送员：' . $bill_master_obj->distribution_technicia,
                	//mod by yim.hu 20141121 FUN-BK-0035-状态不做修改 --start
                		//'extend_5' => 5,
                    'extend_5' => $bill_status,
                	//mod by yim.hu 20141121 FUN-BK-0035-状态不做修改 --end                		
                    //mod by allen.qiang 20141023 系统中操作信息中“操作人”切换为中文 --start
                    //'operator' => $G_PGLOB->currentUserID
                    'operator' => $G_PGLOB->currentUserName
                    //mod by allen.qiang 20141023 系统中操作信息中“操作人”切换为中文 --end
                );

                $result_array = $logHandler->saveLogInfo( $log_array );
                if ($result_array['code'] != 200){
                    throw new Exception( $result_array['message'], 400);
                }
                /*=======================================================
                 * 写入日志 end
                 * ======================================================
                 */
            }


            //提交事务
            $G_PGLOB->DBCA->commit();
            returnJsonData( 'ok', 200 );

        }catch (Exception $e){

            //回滚事务
            $G_PGLOB->DBCA->rollBack();

            if ($e->getCode() > 0) {
                returnJsonData( $e->getMessage(),$e->getCode());
            } else {
                returnJsonData( '系统错误，请与管理员联系',500);
            }

        }

    }


    /*
   *
   * 分配修改 根据配送站id查询订单主表信息 校验是否分配配送站 是否分配配送员
   */
    public function billMasterSearchEdit(){

        global $G_PGLOB;

        if(!isset($_REQUEST['billNoArr'])){
            returnJsonData('缺少参数',400);
        }

        if(!is_array($_REQUEST['billNoArr'])){
            returnJsonData('参数错误',400);
        }

        $notFoundArr = array();//未找到该订单号数组
        $notAllocationArr = array();//未分配配送站数组
        $notMatchingArr = array();//查询条件中配送站与列表中配送站不匹配数组
        $yesAllocationArr = array();//已分配配送站数组


        foreach($_REQUEST['billNoArr'] as $bill_no){

            //实例化订单主表
            $bill_master_obj = new entity('bill_master');
            $bill_master_obj->bill_no =$bill_no;

            if(!$G_PGLOB->DBCA->fetch($bill_master_obj)){
                $notFoundArr[] = $bill_no;
            }

            if(empty($bill_master_obj->distribution_id) || $bill_master_obj->distribution_id == 0){
                $notAllocationArr[] = $bill_no;
            }

            if(!isset($bill_master_obj->distribution_technician_id) || $bill_master_obj->distribution_technician_id == 0){
                $yesAllocationArr[] = $bill_no;
            }

            if($bill_master_obj->distribution_id != $_REQUEST['distribution_id']){
                $notMatchingArr[] = $bill_no;
            }

        }

        if( $notFoundArr ){
            $res = implode('], [', $notFoundArr);
            returnJsonData('订单号['. $res . ']未找到该订单信息',400);
        }

        if( $notAllocationArr ){
            $res = implode('], [', $notAllocationArr);
            returnJsonData('订单号['. $res . ']未分配配送站，不能分配修改',400);
        }

        if( $yesAllocationArr ){
            $res = implode('], [', $yesAllocationArr);
            returnJsonData('订单号['. $res . ']未分配配送员，不能分配修改',400);
        }

        if( $notMatchingArr ){
            $res = implode('], [', $notMatchingArr);
            returnJsonData('订单号['. $res . ']查询条件中配送站与列表中配送站不匹配，请在查询条件里选择城市和配送站进行查询后再使用本操作',400);
        }

        $distribution_technician_array = array(
            'distribution_technician_id'=>$bill_master_obj->distribution_technician_id,//配送员
            'distribution_technicia'=>$bill_master_obj->distribution_technicia
        );
        returnJsonData('ok',200,$distribution_technician_array);
    }


    /*
     *
     * 执行分配修改配送员
     */
    public function editDistribution_technician(){

        global $G_PGLOB;

        if(!isset($_REQUEST['distribution_technician_id'],$_REQUEST['mobile'],$_REQUEST['billNoArr'],$_REQUEST['distribution_technicia'])){

            returnJsonData('缺少参数',400);
        }

        if(!is_numeric($_REQUEST['distribution_technician_id'])){
            returnJsonData('参数错误',400);
        }

        if(!is_array($_REQUEST['billNoArr'])){
            returnJsonData('参数错误',400);
        }

        //实例化配送员类 校验配送员信息
        $sysDistributionTechnician = new sysDistributionTechnician();

        $arg_array =  array(
            'distribution_technician_id'=>$_REQUEST['distribution_technician_id'],
            'mobile'=>$_REQUEST['mobile']
        );
        $result = $sysDistributionTechnician->search( $arg_array );

        if(empty($result)){
            returnJsonData('未找到对应配送员信息',400);
        }
        try{
            //开启事务
            $G_PGLOB->DBCA->beginTransaction();

            foreach($_REQUEST['billNoArr'] as $bill_no){

                //实例化订单主表
                $bill_master_obj = new entity('bill_master');
                $bill_master_obj->bill_no =$bill_no;
                if(!$G_PGLOB->DBCA->fetch($bill_master_obj)){
                    throw new Exception('未找到该订单主表信息',400);
                }

                if($bill_master_obj->bill_status == 7){
                    throw new Exception('已完成订单，无需分配',400);
                }
                $bill_master_obj->distribution_technician_id = $_REQUEST['distribution_technician_id'];
                $bill_master_obj->distribution_technicia = $_REQUEST['distribution_technicia'];
                $G_PGLOB->DBCA->save($bill_master_obj);

          /*=======================================================
           * 写入日志 start
           * ======================================================
           */
                quanli_include('module/base/customer/logHandler.class.php');
                //实例日志类
                $logHandler = new logHandler( $G_PGLOB->DBCA );

                $log_array = array(
                    'logCategoryID' => 1,
                    'logItem' => $bill_master_obj->bill_no,
                    'extend_1' => '修改配送员',
                    'extend_2' => '配送员修改为：'.$bill_master_obj->distribution_technicia,
                    'extend_5' => $bill_master_obj->bill_status,
                    //mod by allen.qiang 20141023 系统中操作信息中“操作人”切换为中文 --start
                    //'operator' => $G_PGLOB->currentUserID
                    'operator' => $G_PGLOB->currentUserName
                    //mod by allen.qiang 20141023 系统中操作信息中“操作人”切换为中文 --end
                );

                $result_array = $logHandler->saveLogInfo( $log_array );
                if ($result_array['code'] != 200){
                    throw new Exception( $result_array['message'], 400);
                }
                /*=======================================================
                 * 写入日志 end
                 * ======================================================
                 */
            }

            //提交事务
            $G_PGLOB->DBCA->commit();
            returnJsonData( 'ok', 200 );

        }catch (Exception $e){

            //回滚事务
            $G_PGLOB->DBCA->rollBack();

            if ($e->getCode() > 0) {
                returnJsonData( $e->getMessage(),$e->getCode());
            } else {
                returnJsonData( '系统错误，请与管理员联系',500);
            }

        }

    }


    /*
 *
 * 分单调度 根据配送站id查询订单主表信息 校验是否分配配送站 是否分配配送员
 */
    public function billMasterSearch_submenu(){

        global $G_PGLOB;

        if(!isset($_REQUEST['billNoArr'])){
            returnJsonData('缺少参数',400);
        }

        if(!is_array($_REQUEST['billNoArr'])){
            returnJsonData('参数错误',400);
        }

        $notFoundArr = array();//未找到该订单号数组
        $notAllocationArr = array();//未分配配送站数组
        $yesAllocationArr = array();//未分配配送员

        $deliver_cityid_array = array(); //城市id数组
        foreach($_REQUEST['billNoArr'] as $bill_no){

            //实例化订单主表
            $bill_master_obj = new entity('bill_master');
            $bill_master_obj->bill_no =$bill_no;

            if(!$G_PGLOB->DBCA->fetch($bill_master_obj)){
                $notFoundArr[] = $bill_no;
            }

            $deliver_cityid_array[] = $bill_master_obj->deliver_cityid;
            if( !in_array( $bill_master_obj->deliver_cityid, $deliver_cityid_array)){
                returnJsonData( '当前选择分单调度的订单城市不一致，请选择同一城市的订单进行分单调度',400);
            }
        }

        if( !empty($notFoundArr) ){
            $res = implode('], [', $notFoundArr);
            returnJsonData('订单号['. $res . ']未找到该订单信息',400);
        }

        returnJsonData('ok',200,$deliver_cityid_array[0]);
    }


    /*
     *
     * 获得配送站信息
     */
    public function getDistribution(){

        //实例化配送站设置类
        $sysDistribution = new sysDistribution( ) ;

        $arg_array = array(
            'cityid'=>$_REQUEST['deliver_cityid']
        );
        $result = $sysDistribution->search( $arg_array ) ;

        if($result){
            returnJsonData('ok',200,$result);
        }else{
            returnJsonData('未找到配送站信息',400);
        }

    }


    /*
     *
     * 分单调度执行
     */
    public function submenuExecute(){

        global $G_PGLOB;

        if(!isset($_REQUEST['distribution_id'],$_REQUEST['billNoArr'])){

            returnJsonData('缺少参数',400);
        }

        if(!is_numeric($_REQUEST['distribution_id'])){
            returnJsonData('参数错误',400);
        }

        if(!is_array($_REQUEST['billNoArr'])){
            returnJsonData('参数错误',400);
        }

        //实例化配送站设置类
        $sysDistribution = new sysDistribution( ) ;

        $arg_array =  array(
            'distribution_id'=>$_REQUEST['distribution_id']
        );

        $result = $sysDistribution->search( $arg_array );

        if(empty($result)){
            returnJsonData('未找到对应配送站信息',400);
        }
        try{
            //开启事务
            $G_PGLOB->DBCA->beginTransaction();

            foreach($_REQUEST['billNoArr'] as $bill_no){

                //实例化订单主表
                //=====================更改订单主表配送站、配送员信息start========================
                $bill_master_obj = new entity('bill_master');
                $bill_master_obj->bill_no = $bill_no;
                if(!$G_PGLOB->DBCA->fetch($bill_master_obj)){
                    throw new Exception('未找到该订单主表信息',400);
                }

                if($bill_master_obj->bill_status == 7){
                    throw new Exception('已完成订单，无需调度',400);
                }
                $distribution_id = $bill_master_obj->distribution_id;
                $distribution_name =$bill_master_obj->distribution_name;
                $bill_master_obj->distribution_id = $_REQUEST['distribution_id'];
                $bill_master_obj->distribution_name = $result[0]['distribution_name'];
                //mod by allen.qiang 20141106 FUN-BK-0023-改配送站清除配送员 start
                //如果本次修改的站点还是原站点，配送员信息不清空。
                if ($distribution_id != $_REQUEST['distribution_id'])
                {
                    $bill_master_obj->distribution_technician_id = '';
                    $bill_master_obj->distribution_technicia = '';
                }
                //mod by allen.qiang 20141106 FUN-BK-0023-改配送站清除配送员 end
                $G_PGLOB->DBCA->save($bill_master_obj);
                //=====================更改订单主表配送站、配送员信息end========================

           /*=======================================================
            * 写入日志 start
            * ======================================================
            */
                quanli_include('module/base/customer/logHandler.class.php');
                //实例日志类
                $logHandler = new logHandler( $G_PGLOB->DBCA );

                $log_array = array(
                    'logCategoryID' => 1,
                    'logItem' => $bill_master_obj->bill_no,
                    'extend_1' => '订单修改配送站',
                    'extend_2' => '配送站修改为：'.$bill_master_obj->distribution_name,
                    'extend_5' => $bill_master_obj->bill_status,
                    //mod by allen.qiang 20141023 系统中操作信息中“操作人”切换为中文 --start
                    //'operator' => $G_PGLOB->currentUserID
                    'operator' => $G_PGLOB->currentUserName
                    //mod by allen.qiang 20141023 系统中操作信息中“操作人”切换为中文 --end
                );

                $result_array = $logHandler->saveLogInfo( $log_array );
                if ($result_array['code'] != 200){
                    throw new Exception( $result_array['message'], 400);
                }
                /*=======================================================
                 * 写入日志 end
                 * ======================================================
                 */


                //实例化订单商品生产表
                //=====================更改订单主表配送站信息start========================
                $arg_array = array(
                    'bill_no'=> $bill_no
                );

                $bill_goods_product = new billProduct();
                $bill_goods_product_array = $bill_goods_product->searchBillProductGoods( $arg_array );


                if( $bill_goods_product_array ){
                    foreach( $bill_goods_product_array as $v){
                        $bill_goods_product_obj = new entity('bill_goods_product');
                        $bill_goods_product_obj->bill_goods_no = $v['bill_goods_no'];
                        if(!$G_PGLOB->DBCA->fetch( $bill_goods_product_obj )){
                            throw new Exception('未找到该订单商品信息',400);
                        }
                        $bill_goods_product_obj->distribution_id = $_REQUEST['distribution_id'];
                        $bill_goods_product_obj->distribution_name = $result[0]['distribution_name'];
                        $G_PGLOB->DBCA->save( $bill_goods_product_obj );
                    }
                }

                //=====================更改订单主表配送站信息end========================



                //===================保存订单修改提示(分单调度)start=========================

                //订单修改对生产部、配送部的通知 保存到 bill_alter_notice

                //查询商品信息
                $arg_array = array(
                    'bill_no' => $bill_no
                );

                //加载类文件
                quanli_include('module/base/bill/billSearch.class.php');
                //实例订单查询类
                $billSearch = new billSearch();
                $bill_goods_array = $billSearch->searchBillGoods($arg_array);
                $goods_array = array();
                foreach($bill_goods_array as $goods){

                    if ($goods_array[$goods['goods_postID']]) {
                        $goods_array[$goods['goods_postID']][2] += $goods['goods_amount'];
                    } else {
                        $goods_array[$goods['goods_postID']] = array(
                            $goods['goods_name'],
                            $goods['goods_spec'],
                            $goods['goods_amount']
                        );
                    }
                }

                $goodsStr = "";

                foreach($goods_array as $data){
                    $goodsStr .= $data[0];
                    $goodsStr .= ",";
                    $goodsStr .= $data[1];
                    $goodsStr .= ",";
                    $goodsStr .= $data[2] ."个";
                    $goodsStr .= ";";
                }

                //实例化订单修改提示表
                $bill_alter_notice = new entity('bill_alter_notice');
                $bill_alter_notice->creator = $arg_array['operator'];
                $bill_alter_notice->createTime = $this->sys_datetime;
                $bill_alter_notice->bill_no = $bill_no;
                $bill_alter_notice->bill_code = $bill_master_obj->bill_code;
                $bill_alter_notice->goods_info = $goodsStr;
                $bill_alter_notice->bill_status = $bill_master_obj->bill_status;
                $bill_alter_notice->city_id = $bill_master_obj->deliver_cityid;
                $bill_alter_notice->distribution_id = $bill_master_obj->distribution_id;
                $bill_alter_notice->distribution_name = $bill_master_obj->distribution_name;
                $bill_alter_notice->alter_content = '订单由'.$distribution_name.'调度到'.$bill_master_obj->distribution_name;
                $bill_alter_notice->send_date = $bill_master_obj->send_date;
                $bill_alter_notice->is_handle = 2;//默认未处理
                $bill_alter_notice->is_allocation = 1;//是分单调度
                $G_PGLOB->DBCA->save( $bill_alter_notice );

                //===================保存订单修改提示(分单调度)end=========================

            }

            //提交事务
            $G_PGLOB->DBCA->commit();
            returnJsonData( 'ok', 200 );

        }catch (Exception $e){

            //回滚事务
            $G_PGLOB->DBCA->rollBack();

            if ($e->getCode() > 0) {
                returnJsonData( $e->getMessage(),$e->getCode());
            } else {
                returnJsonData( '系统错误，请与管理员联系',500);
            }

        }

    }

}